//
//  FindViewController.m
//  ClockIn v2
//
//  Created by Tomy on 12/11/2020.
//

#import "FindViewController.h"
#import "LoadingTableViewCell.h"
#import "Utility.h"
#import "Masonry.h"
#import "WatchViewController.h"
#import "LoadedTableViewCell.h"

@interface FindViewController () <UITextFieldDelegate>{
    NSMutableArray *TextdataArray;
    NSInteger numberItem;
    UITableView *list;
}

@property(nonatomic, assign) LoadingStatus status;

@end


@implementation FindViewController

- (void)viewDidLoad{
    [super viewDidLoad];
    
    self.navigationItem.title = @"打卡清单";
    [Utility addGradientLayerToView:self.view];
    
    // 搜索框
    UITextField *search = [UITextField new];
    search.placeholder = @"搜索框";
    search.borderStyle = UITextBorderStyleRoundedRect;
    search.delegate = self;
    
    [self.view addSubview:search];
    CGRect navRect = self.navigationController.navigationBar.frame;
    //NSLog(@"height %f", );
    [search mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(0.95 * self.view.bounds.size.width);
            make.height.mas_equalTo(30);
            //make.top.equalTo(self.view).offset([Utility getTopBarWidth] + 10);
            //make.top.equalTo(self.navigationController.view.bounds.size.height).offset(10);
            make.top.equalTo(self.view).offset(navRect.size.height + 60);
            make.centerX.equalTo(self.view.mas_centerX);
        }];
    
    
    // 初始化数据
    TextdataArray = [NSMutableArray new];
    for (int i = 20; i >= 0; i--) {
        NSString *date = [NSString stringWithFormat:@"%04d-%02d-%02d", 2000 + i, i % 12 + 1, i % 11 + 10];
        NSString *place = [NSString stringWithFormat:@"地点 %d", i];
        NSString *scene = [NSString stringWithFormat:@"景点名称 %d", i];
        NSString *experience = [NSString stringWithFormat:@"心得 %d", i];

        [TextdataArray addObject:@[date, place, scene, experience]];
    }
    numberItem = 8;
    
    
    // 列表
    list = [UITableView new];
    list.delegate = self;
    list.dataSource = self;
    list.backgroundColor = [UIColor clearColor];
    list.separatorStyle = UITableViewCellSelectionStyleNone;
    [self.view addSubview:list];
    
    [list mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(0.95 * self.view.bounds.size.width);
            make.top.equalTo(search.mas_bottom).offset(10);
            make.bottom.equalTo(self.view).offset(-self.tabBarController.tabBar.frame.size.height - 20);
            make.centerX.equalTo(self.view.mas_centerX);
    }];
//    [_list mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.width.mas_equalTo(0.95 * self.view.bounds.size.width);
//        make.top.equalTo(search.mas_bottom).offset(10);
//        make.bottom.equalTo(self.view).offset(-self.tabBarController.tabBar.frame.size.height - 20);
//        make.centerX.equalTo(self.view.mas_centerX);
//    }];
    
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return numberItem + 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *reuse1 = @"WatchViewController";
    static NSString *reuse2 = @"LoadingTableViewCell";
    UITableViewCell *cell;
    if (indexPath.row < numberItem) {
        cell = [tableView dequeueReusableCellWithIdentifier:reuse1];
        if (cell == nil) {
            cell = [[LoadedTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse1];
        }
        [(LoadedTableViewCell*)cell setupWithDate:TextdataArray[indexPath.row][0] place:TextdataArray[indexPath.row][1] experience:TextdataArray[indexPath.row][3]];
    }
    else {
        cell = [tableView dequeueReusableCellWithIdentifier:reuse2];
        if (cell == nil) {
            cell = [[LoadingTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse2];
        }
        ((LoadingTableViewCell*)cell).status = self.status;
    }
    
    cell.backgroundColor = [UIColor clearColor];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row < numberItem) {
        return 100;
    }
    return 60;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row < numberItem) {
        NSArray *infoDetail = TextdataArray[indexPath.row];
        WatchViewController *info = [[WatchViewController alloc]initWithDate:infoDetail[0] place:infoDetail[1] attraction:infoDetail[2] experience:infoDetail[3]];
        info.navigationItem.title = @"查看打卡";
        [Utility addGradientLayerToView:info.view];
        [self.navigationController pushViewController:info animated:YES];
        }
        else {
            if (self.status == LoadingStatusDefault) {
                self.status = LoadingStatusLoding;
                [tableView reloadData];
                
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    self->numberItem = MIN(self->numberItem + 4, self->TextdataArray.count);
                    if (self->numberItem < self->TextdataArray.count) {
                        self.status = LoadingStatusDefault;
                    }
                    else {
                        self.status = LoadingStatusNoMore;
                    }
                    
                    [tableView reloadData];
                });
            }
        }}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"搜索" message:@"检索完成" preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"了解" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        return;
    }]];
    [self presentViewController:alert animated:YES completion:nil];
    return YES;
}

- (void)addNewMarkWithDate:(NSString *)date place:(NSString *)place interest:(NSString *)interest experience:(NSString *)experience{
    ++numberItem;
    [TextdataArray insertObject:@[date, place, interest, experience] atIndex:0];
    [list reloadData];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self->list scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:true];
    });
}



@end
